package com.cdtu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cdtu.dto.OrderDTO;
import com.cdtu.dto.R;
import com.cdtu.entity.*;
import com.cdtu.service.OrderService;
import com.cdtu.service.SightsService;
import com.cdtu.service.TicketService;
import com.cdtu.service.UserService;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : Sakura
 * @Date : 2023/10/29 11:22
 * @Description : TODO 用来接收...的请求
 */
@RestController
@RequestMapping("/api/order")
//解决跨域问题
@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private TicketService ticketService;

    @Autowired
    private UserService userService;

    @Autowired
    private SightsService sightsService;
    @GetMapping("/list/{pageNow}/{pageSize}/{uid}")
    public R list(@PathVariable("pageNow") Integer pageNow,
                  @PathVariable("pageSize") Integer pageSize,
                  @PathVariable("uid") Long uid){
        Page<OrderDTO> pageDTO = new Page<>(pageNow, pageSize);
        LambdaQueryWrapper<Sights> sightWrapper = new LambdaQueryWrapper<>();
        sightWrapper.eq(Sights::getPublishUser, uid);
        List<Sights> sightsList = sightsService.list(sightWrapper);
//        拿出所有的商家发布的sightId;
        List<Long> sightIdList = sightsList.stream().map(item -> item.getId()).collect(Collectors.toList());
//        再去拿到所有景点相关的票
        if (sightIdList.size() == 0) {
            return R.success(pageDTO);
        }
        LambdaQueryWrapper<Ticket> ticketWrapper = new LambdaQueryWrapper<>();
        ticketWrapper.in(sightIdList.size() > 0, Ticket::getSightId, sightIdList);
        List<Ticket> ticketList = ticketService.list(ticketWrapper);

        //拿出所有的ticketId
        List<Long> ticketIdList = ticketList.stream().map(item -> item.getId()).collect(Collectors.toList());
        if (ticketIdList.size() == 0) {
            return R.success(pageDTO);
        }
        LambdaQueryWrapper<Order> orderWrapper = new LambdaQueryWrapper<>();
        orderWrapper.in(ticketIdList.size() > 0, Order::getTicketId, ticketIdList);

//        正经查询
        Page<Order> page = orderService.page(new Page<>(pageNow, pageSize), orderWrapper);
        //封装dto对象
        getDTOPage(pageDTO, page);

        return R.success(pageDTO);
    }

    private void getDTOPage(Page<OrderDTO> pageDTO, Page<Order> page) {
        List<OrderDTO> orderDTOList = page.getRecords().stream().map(item -> {
            OrderDTO dto = new OrderDTO();
            BeanUtils.copyProperties(item, dto);
            Ticket ticket = ticketService.getById(item.getTicketId());
            Sights sights = sightsService.getById(ticket.getSightId());
            dto.setSightName(sights.getName());
            User user = userService.getById(item.getUserId());
            dto.setUserName(user.getName());
            dto.setPhone(item.getPhone());
            dto.setTicketName(ticket.getName());
            dto.setPrice(ticket.getPrice());
            return dto;
        }).collect(Collectors.toList());
        BeanUtils.copyProperties(page, pageDTO, "records");
        pageDTO.setRecords(orderDTOList);
    }

    /**
     * 查询我的订单
     * @param pageNo:
     * @param pageSize:
     * @param uid:
     * @return R
     * @author Sakura
     * @date 2023/10/29 15:32
     */
    @GetMapping("/my/{pageNo}/{pageSize}/{uid}")
    public R myOrder(@PathVariable("pageNo") Integer pageNo,
                     @PathVariable("pageSize") Integer pageSize,
                     @PathVariable("uid") Long uid) {
        LambdaQueryWrapper<Order> orderWrapper = new LambdaQueryWrapper<>();
        orderWrapper.eq(Order::getUserId, uid);
        Page<OrderDTO> pageDTO = new Page<>(pageNo, pageSize);
        Page<Order> page = orderService.page(new Page<>(pageNo, pageSize), orderWrapper);
        getDTOPage(pageDTO, page);
        return R.success(pageDTO);
    }

    @PutMapping("/cancel/{oid}")
    public R getById(@PathVariable("oid") long oid){
        Order order = orderService.getById(oid);
        order.setCancel(1);
        boolean update = orderService.updateById(order);
        if (!update) {
            return R.error("取消失败,请重新尝试");
        }
        return R.success("取消成功");
    }
}
